package Core;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class RecordManager
{
	public RecordManager()
	{
	}

	public static void setRecord(String key, int val)
	{
		RecordStore recordStore = null;
		Integer i = new Integer(val);
		
		try
		{
			recordStore = RecordStore.openRecordStore(key, true);
		}
		catch (RecordStoreException ex)
		{
			return;
		}
		
		try
		{
			recordStore.setRecord(1, i.toString().getBytes(), 0, i.toString().getBytes().length);
		} 
		catch (RecordStoreException ex)
		{
			try
			{
				recordStore.addRecord(i.toString().getBytes(), 0, i.toString().getBytes().length);
			} 
			catch (RecordStoreException ex1)
			{
				//ex1.printStackTrace();
			}
		}
	}

	public static int getRecord(String key, int def)
	{
		RecordStore recordStore = null;
		
		try
		{
			recordStore = RecordStore.openRecordStore(key, false);
		}
		catch (RecordStoreException ex)
		{
			return def;
		}
		
		try
		{
			byte[] arr = recordStore.getRecord(1);
			String str = new String(arr);
			return Integer.valueOf(str).intValue();
		} 
		catch (RecordStoreException ex)
		{
			return def;
		}
		catch (NumberFormatException ex)
		{
			//ex.printStackTrace();
			return def;
		}
	}
}
